<?php
namespace app\admin\common\model;


use think\Model;

class User extends Model
{
    protected $pk = 'id';
    protected $table = 'qi_user';

    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    protected $dateFormat = 'Y-m-d H:i:s';


    public function getInfoById($id)
    {
        $info = User::where('id',$id)->find();
        return $info;
    }

    public function getPageList($pageSize=10)
    {
        $list = User::order('id','desc')->paginate($pageSize);
        return $list;
    }

    /*
     * 更新会员
     */
    public function editUser($info)
    {
        $result = User::update($info);
        return $result;
    }

    /*
     * 删除会员
     * 先将对应的图片删除
     */
    public function delUser($id)
    {
        $this->delAvatar($id);

        $result = User::where('id',$id)->delete();
        return $result;
    }

    /*
     * 删除id对应的图片
     */
    public function delAvatar($id)
    {
        $avatar = User::where('id',$id)->value('avatar');

        if (!empty($avatar)) {
            $fileName = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/' . $avatar;

            if (file_exists($fileName)) {
                unlink($fileName);
            }
        }
    }
}